home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / LGP250S1.ZIP / src / libgplus.5 / libgplus / tests / tfix16.cc < prev    next >
C/C++ Source or Header  |  1993-06-06  |  3KB  |  111 lines

  1. //
  2. // testFix16.cc : test Fix16/32 classes
  3. //
  4.  
  5. #include <Fix16.h>
  6.  
  7. // This set of inlines (instead of a macro) is to force the side effects
  8. // of evaluating y to happen before x is printed.
  9.  
  10. inline void check(char *x, int y) { cout << x << " = " << (y) << "\n"; }
  11. inline void check(char *x, long y) { cout << x << " = " << (y) << "\n"; }
  12. inline void check(char *x, double y) { cout << x << " = " << (y) << "\n"; }
  13. inline void check(char *x, Fix16 y) { cout << x << " = " << (y) << "\n"; }
  14. inline void check(char *x, Fix32 y) { cout << x << " = " << (y) << "\n"; }
  15.  
  16. void test16() {
  17.   cout << "Fix16: identities should be displayed\n";
  18.  
  19.   Fix16 a;        check("0",a);
  20.   Fix16 b = .5;        check(".5",b);
  21.   Fix16 c = -.5;    check("-.5",c);
  22.   Fix16 d = .1;        check(".1",d);
  23.   Fix16 e = b;        check(".5",e);
  24.  
  25.   check(".5",a = b);
  26.   check(".25",a = .25);
  27.   check("8192",mantissa(a));
  28.   mantissa(a)=8192;
  29.   check(".25",a);
  30.   check(".25",value(a));
  31.  
  32.   check(".25",+a);
  33.   check("-.25",-a);
  34.  
  35.   check(".1 + .5",d+b);
  36.   check(".1 - .5",d-b);
  37.   check(".1 * .5",d*b);
  38.   check(".1 *  3",d*3);
  39.   check(".1 * -3",d*-3);
  40.   check(".1 / .5",d/b);
  41.   check(".1 << 1",d<<1);
  42.   check("-.5 >> 2",c>>2);
  43.  
  44.   check(".1 == .5",d == b);
  45.   check(".1 != .5",d != b);
  46.   check(".1 > .5",d > b);
  47.   check(".5 <= -.5",b <= c);
  48.  
  49.   cout << "Fix16: range errors ignored and overflows saturated\n";
  50.   set_Fix16_overflow_handler(Fix16_overflow_saturate);
  51.   set_Fix16_range_error_handler(Fix16_ignore);
  52.  
  53.   Fix16 f = 1.1;    check("1.1",f);
  54.  
  55.   Fix16 g = .7;
  56.   check(".7 + .5",g+b);
  57.   check("-.5 - .7",c-g);
  58.   check(".5 / .1",b/d);
  59. }
  60.  
  61. void test32() {
  62.   cout << "Fix32: identities should be displayed\n";
  63.  
  64.   Fix32 a;        check("0",a);
  65.   Fix32 b = .5;        check(".5",b);
  66.   Fix32 c = -.5;    check("-.5",c);
  67.   Fix32 d = .1;        check(".1",d);
  68.   Fix32 e = b;        check(".5",e);
  69.  
  70.   check(".5",a = b);
  71.   check(".25",a = .25);
  72.   check("536870912",mantissa(a));
  73.   mantissa(a)=536870912;
  74.   check(".25",a);
  75.   check(".25",value(a));
  76.  
  77.   check(".25",+a);
  78.   check("-.25",-a);
  79.  
  80.   check(".1 + .5",d+b);
  81.   check(".1 - .5",d-b);
  82.   check(".1 * .5",d*b);
  83.   check(".1 *  3",d*3);
  84.   check(".1 * -3",d*-3);
  85.   check(".1 / .5",d/b);
  86.   check(".1 << 1",d<<1);
  87.   check("-.5 >> 2",c>>2);
  88.  
  89.   check(".1 == .5",d == b);
  90.   check(".1 != .5",d != b);
  91.   check(".1 > .5",d > b);
  92.   check(".5 <= -.5",b <= c);
  93.  
  94.   cout << "Fix32: range errors reported and overflows reported\n";
  95.   set_Fix32_overflow_handler(Fix32_warning);
  96.   set_Fix32_range_error_handler(Fix32_warning);
  97.  
  98.   Fix32 f = 1.1;    check("1.1",f);
  99.  
  100.   Fix32 g = .7;
  101.   check(".7 + .5",g+b);
  102.   check("-.5 - .7",c-g);
  103.   check(".5 / .1",b/d);
  104. }
  105.  
  106. int main() {
  107.   test16();
  108.   test32();
  109.   return 0;
  110. }
  111.